package com.igoal.dto;

import java.io.Serializable;
import java.util.Date;

import com.igoal.entity.Message;
import com.igoal.entity.User;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;

public class MessageDTO extends BaseDTO<Message> implements Serializable {

	private static final long serialVersionUID = -6564620366838079904L;

	private Long fromId;
	private Long toId;
	private String fromName;
	private String fromAvatar;
	private String bodyTooltip;
	private String subject;
	private String body;
	private boolean messageStatus;
	private Long sentSince;

	public Long getFromId() {
		return fromId;
	}

	public void setFromId(Long userByFromId) {
		this.fromId = userByFromId;
	}

	public Long getToId() {
		return toId;
	}

	public void setToId(Long userByToId) {
		this.toId = userByToId;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
		if(this.body != null){
			if(this.body.length() > 20){//TODO: remove hardcode
				this.bodyTooltip = this.body.substring(0, 20);
			}else{
				this.bodyTooltip = this.body;
			}
		}
	}

	public void setFromAvatar(String fromAvatar) {
		this.fromAvatar = fromAvatar;
	}

	public String getFromAvatar() {
		return fromAvatar;
	}

	public Long getSentSince() {
		return sentSince;
	}

	public void setSentSince(Long sentSince) {
		this.sentSince = sentSince;
	}

	public String getFromName() {
		return fromName;
	}

	public void setFromName(String fromName) {
		this.fromName = fromName;
	}

	public String getBodyTooltip() {
		return bodyTooltip;
	}

	public void setBodyTooltip(String bodyTooltip) {
		this.bodyTooltip = bodyTooltip;
	}

	public boolean isMessageStatus() {
		return messageStatus;
	}

	public void setMessageStatus(boolean messageStatus) {
		this.messageStatus = messageStatus;
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "to")
	public void initUserToFromEntity(Object entity) {
		if (entity != null && entity instanceof User) {
			User user = (User) entity;
			this.toId = user.getId();			
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "from")
	public void initUserFromFromEntity(Object entity) {
		if (entity != null && entity instanceof User) {
			User user = (User) entity;
			this.fromId = user.getId();
			this.fromAvatar = user.getAvatar();
			this.fromName = user.getUsername();
		}
	}
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "createDate")
	public void initSentSinceFromEntity(Object date){
		if(date != null && date instanceof Date){
			Date createDate = (Date) date;
			sentSince = CalendarUtils.calculateMinutesDifferentBeforeNow(createDate);
		}
	}
}
